<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import audio from '@/assets/audios/letter.mp3'
import { queryCurrentLetter } from '@/api/letter.ts'

const isAudio = ref<boolean>(false)
const audioRef = ref<HTMLAudioElement | null>(null)

onMounted(() => {
    if (!audioRef.value) {
        audioRef.value = new Audio(audio)
    }
})

const audioChange = (value: string | number | boolean) => {
    if (!audioRef.value) return
    if (value) {
        audioRef.value.play()
    } else {
        audioRef.value.pause()
    }
}

onUnmounted(() => {
    /**
     * 这里要先停止播放再设置为 null
     * 使用 Audio() 构造函数创建的 audio 元素被删除，
     * 根据 JavaScript 垃圾回收机制，如果播放正在进行，
     * 内存中的 audio 元素不会被移除。
     * @See: https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLAudioElement/Audio
     */
    if (audioRef.value) {
        audioRef.value.pause()
        audioRef.value = null
    }
})

// 获取信件内容
const textNode = ref<HTMLSpanElement | null>(null)
let timer: number | null = null
onMounted(async () => {
    if (!textNode.value) return
    const result = await queryCurrentLetter()
    typeWriter(textNode.value, result.content, 100)
})

function typeWriter(el: HTMLSpanElement, str = '美好如约而至!', delay = 100) {
    let index = 0
    timer = setInterval(() => {
        el.innerHTML = str.slice(0, index)
        index++
        if (str.length + 1 === index) clearInterval(timer as number)
    }, delay)
}
</script>

<template>
    <div class="letter-container base-card">
        <div class="head">
            <div class="head-text">背景音乐</div>
            <el-switch
                v-model="isAudio"
                inline-prompt
                active-text="开"
                inactive-text="关"
                @change="audioChange"
            />
        </div>
        <div class="letter-cont">
            <div class="cont-head">一封信</div>
            <div class="cont-card">
                <p class="text-container">
                    <span
                        class="text"
                        ref="textNode"
                    >
                    </span>
                    <span class="cursor"> |</span>
                </p>
            </div>
        </div>
    </div>
</template>

<style scoped lang="scss">
@keyframes cursor-animation {
    0% {
        opacity: 1;
    }
    // 50% {
    //     opacity: 0;
    // }
    100% {
        opacity: 0;
    }
}
.letter-container {
    background-image: url('../../assets/svg/bear.svg');
    background-position: center center;
    background-size: 80px;

    .head {
        width: 100%;
        height: 34px;
        display: flex;
        justify-content: flex-end;
        align-items: center;

        .head-text {
            height: 34px;
            display: flex;
            align-items: center;
            margin-right: 12px;
            color: var(--textColorPrimary);
            font-size: 12px;
        }
    }

    .letter-cont {
        width: 100%;
        height: calc(100% - 40px);
        padding: 23px;
        color: var(--textColorPrimary);
        overflow: auto;

        .cont-head {
            width: 100%;
            height: 30px;
            text-align: center;
            font-size: 20px;
            font-weight: 500;
        }

        .cont-card {
            margin-top: 20px;
            padding: 0 20%;

            > .text-container {
                line-height: 34px;
            }
        }
    }

    .cursor {
        animation-name: cursor-animation;
        animation-duration: 1s;
        animation-iteration-count: infinite;
        animation-fill-mode: none;
        animation-timing-function: ease-in-out;
        opacity: 1;
    }
}
</style>
